﻿using System;
using System.Linq;
using System.Web.Http;
using ScrambledEggs.Entities;

namespace ScrambledEggs.Controllers.Api
{
    public class SuggestionsController : ApiController
    {
        private readonly ScrambledEggsEntities _entities;

        public SuggestionsController()
        {
            // ReSharper disable EmptyGeneralCatchClause
            try
            {
                var conn = System.Configuration.ConfigurationManager.ConnectionStrings["ScrambledEggsConnection"].ConnectionString;
                _entities = new ScrambledEggsEntities(conn);
            }
            catch (Exception)
            {

            }
            // ReSharper restore EmptyGeneralCatchClause
        }

        public void Put(SuggestionModel model)
        {
            _entities.Suggestions.Add(new Suggestion {
                Save = _entities.Saves.SingleOrDefault(s => s.StateKey == model.SaveKey),
                Date = DateTime.Now,
                Text = model.Text,
            });
            _entities.SaveChanges();
        }

        public class SuggestionModel
        {
            public Guid SaveKey { get; set; }
            public String Text { get; set; }
        }
    }
}